﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL;
using System.Collections;
using ASPNETVN.PORTAL.Components.Configuration;
using ASPNETVN.PORTAL.Components.Configuration.Settings;
using ASPNETVN.PORTAL.Components.UI.DataTypes;
using System.Data;

namespace KLP.Modules.HOME
{
    public partial class Main : PortalModuleControl
    {
        public Main()
        {
            ModuleSettingItem itemSetting = new ModuleSettingItem(new StringDataType());
            itemSetting.Value = Guid.Empty.ToString();
            itemSetting.Name = "ModuleID của Module CMS";
            itemSetting.Description = "ModuleID của Module CMS";
            itemSetting.SortOrder = 1;
            this.BaseSettings.Add("CMS.ModuleID", itemSetting);

            // Define an Array List
            ArrayList list = new ArrayList();
            list.Add(new ListItem("Style1", "Style1"));
            list.Add(new ListItem("Style2", "Style2"));
            list.Add(new ListItem("Style3", "Style3"));
            list.Add(new ListItem("Style4", "Style4"));
            list.Add(new ListItem("Style5", "Style5"));

            itemSetting = new ModuleSettingItem(new CustomListDataType(list, "Text", "Value"));
            itemSetting.Value = "Style1";
            itemSetting.Name = "Hình thức hiển thị nội dung";
            itemSetting.Description = "Hình thức hiển thị nội dung ở trang chủ";
            itemSetting.SortOrder = 1;
            this.BaseSettings.Add("DisplayStyle", itemSetting);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Hashtable settings = this.Settings;
                Guid moduleID = new Guid(settings["CMS.ModuleID"].ToString());

                NewsWindow.Attributes.Add("src", Global.GetApplicationPath() + "/Modules/KLP.Modules.HOME/ScrollingContent.aspx?mid=" + moduleID);// + "&top=" + this.Settings["TopRecords"].ToString());
                kitco24h.Attributes.Add("src", Global.GetApplicationPath() + "/Modules/KLP.Modules.CHART/Pages/Kitco24hChart.aspx");
                kitco30d.Attributes.Add("src", Global.GetApplicationPath() + "/Modules/KLP.Modules.CHART/Pages/Kitco30dChart.aspx");
                kitcousd.Attributes.Add("src", Global.GetApplicationPath() + "/Modules/KLP.Modules.CHART/Pages/KitcoUsdChart.aspx");
                Ads.Attributes.Add("src", Global.GetApplicationPath() + "/Modules/KLP.Modules.ADVERTISEMENT/Style1.html");

                

                // Get one type item for home.
                Repeater1.DataSource = ASPNETVN.PORTAL.Modules.CMS.Components.Item.GetItemsByDisplayType(moduleID, "ONE");
                Repeater1.DataBind();

                // Get two type item for home.
                Repeater2.DataSource = ASPNETVN.PORTAL.Modules.CMS.Components.Item.GetItemsByDisplayType(moduleID, "TWO");
                Repeater2.DataBind();

                // Get KLP.TTNH.TV type item for home.
                DataTable dt = ASPNETVN.PORTAL.Modules.CMS.Components.Item.GetItemsByDisplayType(moduleID, "KLP.TTNH.TV").Tables[0];
                var query = (from r in dt.AsEnumerable()
                              select new { ID = r.Field<int>("ID"), Title = r.Field<string>("Title"), SubTitle = r.Field<string>("SubTitle"), PublishedDate = r.Field<DateTime>("PublishedDate") }).OrderByDescending(x => x.PublishedDate).Take(1);
                RepeaterTTNH_TV.DataSource = query;
                RepeaterTTNH_TV.DataBind();

                // Get KLP.TTNH.KTGD type item for home.
                dt = ASPNETVN.PORTAL.Modules.CMS.Components.Item.GetItemsByDisplayType(moduleID, "KLP.TTNH.KTGD").Tables[0];
                query = (from r in dt.AsEnumerable()
                              select new { ID = r.Field<int>("ID"), Title = r.Field<string>("Title"), SubTitle = r.Field<string>("SubTitle"), PublishedDate = r.Field<DateTime>("PublishedDate") }).OrderByDescending(x => x.PublishedDate).Take(1);
                RepeaterTTNH_KTGD.DataSource = query;
                RepeaterTTNH_KTGD.DataBind();

                // Get KLP.TTNH.NDCG type item for home.
                dt = ASPNETVN.PORTAL.Modules.CMS.Components.Item.GetItemsByDisplayType(moduleID, "KLP.TTNH.NDCG").Tables[0];
                query = (from r in dt.AsEnumerable()
                         select new { ID = r.Field<int>("ID"), Title = r.Field<string>("Title"), SubTitle = r.Field<string>("SubTitle"), PublishedDate = r.Field<DateTime>("PublishedDate") }).OrderByDescending(x => x.PublishedDate).Take(1);
                RepeaterTTNH_NDCG.DataSource = query;
                RepeaterTTNH_NDCG.DataBind();

                // -------------------

                // Get KLP.TTBDS.TV type item for home.
                dt = ASPNETVN.PORTAL.Modules.CMS.Components.Item.GetItemsByDisplayType(moduleID, "KLP.TTBDS.TV").Tables[0];
                query = (from r in dt.AsEnumerable()
                             select new { ID = r.Field<int>("ID"), Title = r.Field<string>("Title"), SubTitle = r.Field<string>("SubTitle"), PublishedDate = r.Field<DateTime>("PublishedDate") }).OrderByDescending(x => x.PublishedDate).Take(1);
                RepeaterTTBDS_TV.DataSource = query;
                RepeaterTTBDS_TV.DataBind();

                // Get KLP.TTBDS.KTGD type item for home.
                dt = ASPNETVN.PORTAL.Modules.CMS.Components.Item.GetItemsByDisplayType(moduleID, "KLP.TTBDS.KTGD").Tables[0];
                query = (from r in dt.AsEnumerable()
                         select new { ID = r.Field<int>("ID"), Title = r.Field<string>("Title"), SubTitle = r.Field<string>("SubTitle"), PublishedDate = r.Field<DateTime>("PublishedDate") }).OrderByDescending(x => x.PublishedDate).Take(1);
                RepeaterTTBDS_KTGD.DataSource = query;
                RepeaterTTBDS_KTGD.DataBind();

                // Get KLP.TTBDS.NDCG type item for home.
                dt = ASPNETVN.PORTAL.Modules.CMS.Components.Item.GetItemsByDisplayType(moduleID, "KLP.TTBDS.NDCG").Tables[0];
                query = (from r in dt.AsEnumerable()
                         select new { ID = r.Field<int>("ID"), Title = r.Field<string>("Title"), SubTitle = r.Field<string>("SubTitle"), PublishedDate = r.Field<DateTime>("PublishedDate") }).OrderByDescending(x => x.PublishedDate).Take(1);
                RepeaterTTBDS_NDCG.DataSource = query;
                RepeaterTTBDS_NDCG.DataBind();


                // -------------------

                // Get KLP.OTO.TV type item for home.
                dt = ASPNETVN.PORTAL.Modules.CMS.Components.Item.GetItemsByDisplayType(moduleID, "KLP.TTOTO.TV").Tables[0];
                query = (from r in dt.AsEnumerable()
                         select new { ID = r.Field<int>("ID"), Title = r.Field<string>("Title"), SubTitle = r.Field<string>("SubTitle"), PublishedDate = r.Field<DateTime>("PublishedDate") }).OrderByDescending(x => x.PublishedDate).Take(1);
                RepeaterTTOTO_TV.DataSource = query;
                RepeaterTTOTO_TV.DataBind();

                // Get KLP.OTO.KTGD type item for home.
                dt = ASPNETVN.PORTAL.Modules.CMS.Components.Item.GetItemsByDisplayType(moduleID, "KLP.TTOTO.KTGD").Tables[0];
                query = (from r in dt.AsEnumerable()
                         select new { ID = r.Field<int>("ID"), Title = r.Field<string>("Title"), SubTitle = r.Field<string>("SubTitle"), PublishedDate = r.Field<DateTime>("PublishedDate") }).OrderByDescending(x => x.PublishedDate).Take(1);
                RepeaterTTOTO_KTGD.DataSource = query;
                RepeaterTTOTO_KTGD.DataBind();

                // Get KLP.OTO.NDCG type item for home.
                dt = ASPNETVN.PORTAL.Modules.CMS.Components.Item.GetItemsByDisplayType(moduleID, "KLP.TTOTO.NDCG").Tables[0];
                query = (from r in dt.AsEnumerable()
                         select new { ID = r.Field<int>("ID"), Title = r.Field<string>("Title"), SubTitle = r.Field<string>("SubTitle"), PublishedDate = r.Field<DateTime>("PublishedDate") }).OrderByDescending(x => x.PublishedDate).Take(1);
                RepeaterTTOTO_NDCG.DataSource = query;
                RepeaterTTOTO_NDCG.DataBind();

                // Charts
                kitco24h.Attributes.Add("src", Global.GetApplicationPath() + "/Modules/KLP.Modules.CHART/Pages/Kitco24hChart.aspx");
                kitco30d.Attributes.Add("src", Global.GetApplicationPath() + "/Modules/KLP.Modules.CHART/Pages/Kitco30dChart.aspx");
                kitcousd.Attributes.Add("src", Global.GetApplicationPath() + "/Modules/KLP.Modules.CHART/Pages/KitcoUsdChart.aspx");
            }
        }

        protected string GetUrl(int itemID)
        {
            Guid tabID = Guid.Empty;
            PortalDataSet ds = PortalConfiguration.GetPortalDataSet();

            foreach (PortalDataSet.TabRow tab in ds.Tab.Rows)
            {
                foreach (PortalDataSet.ModuleRow module in tab.GetModuleRows())
                {
                    if (module.ID == new Guid(this.Settings["CMS.ModuleID"].ToString()))
                    {
                        return string.Format("~/Default.aspx?tabid={0}&mid={1}&itemid={2}&page=detail", tab.ID, module.ID, itemID);
                    }
                }
            }
            return string.Format("~/Default.aspx");
        }
    }
}